#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File   ：db-toolkit -> setup
@IDE    ：PyCharm
@Author ：Json
@Date   ：2025/3/25 14:09
@Desc   ：
=================================================='''
# !/usr/bin/env python
from setuptools import setup, find_packages
from setuptools.command.install import install
from pathlib import Path
import shutil
import os


class PostInstallCommand(install):
    """安装后生成默认配置文件"""

    def run(self):
        print('开始安装')
        # 先执行标准安装
        install.run(self)

        # 配置文件生成逻辑
        config_path = Path.cwd() / "settings.py"
        if not config_path.exists():
            try:
                # 从包内复制模板
                template_path = Path(__file__).parent / "db_toolkit" / "templates" / "settings.py.j2"
                shutil.copy(template_path, config_path)
                print("\n[SUCCESS] 已生成配置文件 settings.py")
                print("请修改以下关键配置：")
                print("- DB_HOST: 数据库地址")
                print("- DB_USER: 数据库用户名")
                print("- DB_PASSWORD: 数据库密码\n")
            except Exception as e:
                print(f"\n[WARNING] 配置文件生成失败: {str(e)}")


# 读取依赖要求
def get_requirements():
    with open('requirements.txt') as f:
        return [line.strip() for line in f if line.strip() and not line.startswith('#')]


setup(
    name="db-toolkit",
    version="0.2.0",
    package_dir={"": "src"},  # 关键配置：指定源码在src目录
    packages=find_packages(where="src"),  # 从src目录查找
    include_package_data=True,  # 确保非py文件被打包
    # 运行时依赖
    # install_requires=get_requirements(),

    # 开发依赖（可选）
    extras_require={
        'dev': [
            "SQLAlchemy>=1.4",
            "pymysql>=1.0",
            "pandas>=1.3",
            "python-dotenv>=0.19"  # 可选环境变量支持
        ]
    },

    # 包含非Python文件
    package_data={
        'db_toolkit': ['templates/*.j2'],
    },

    # 安装后命令
    cmdclass={
        'install': PostInstallCommand,
    },

    # 元数据
    author="Jason",
    author_email="bomibatterylzs@gmail.com",
    description="Database Toolkit with Auto-Configuration",
    # long_description=open('README.md').read(),
    long_description_content_type="text/markdown",
    url="https://gitee.com/deepliu_2020/db-toolkit",
    classifiers=[
        "Programming Language :: Python :: 3.7+",
        "License :: OSI Approved :: MIT License",
    ],
    python_requires=">=3.7",
)
